<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Session;
class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $newgoods = DB::table('goods')->where('goods_status',0)->where('new',1)->get();
        $hotgoods = DB::table('goods')->where('goods_status',0)->where('hot',1)->get();
        /*$data = DB::table('cat')->where('pid',0)->get();
        $data1 = DB::table('cat')->get();*/
        return view('home.index',['new'=>$newgoods,'hot'=>$hotgoods]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function search(Request $request)
    {
        //搜索书名 模糊查询
        $str = '%'.$request->goods_name.'%';
        $data = DB::table('goods')->where('goods_name','like',$str)->get();
        //dd($data);
        return view('home.search_list',['goods'=>$data]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $user = Session::get('user')[0];
        $data1 = DB::table('comment') ->where('comment.goods_id','=',$id)->get();
        $newgoods = DB::table('goods')->where('goods_status',0)->where('new',1)->get();
        $data = DB::table('goods')->where('id',$id)->first();
        return view('home.goods_show',['val'=>$data,'new'=>$newgoods,'com'=>$data1,'user'=>$user]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function subcat($id)
    {
        //
        $data = DB::table('goods')->where('goods_status',0)->where('cat_id',$id)->get();
        //dd($data);
        return view('home.subcat_list',['subcat'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function allcat($id)
    {
        //先获取下级栏目的所有信息
        $data = DB::table('cat')->where('pid',$id)->get();
        //遍历获取所有的商品
        static $goods = [];
        foreach($data as $val){
            $arr = DB::table('goods')->where('goods_status',0)->where('cat_id',$val->id)->get();
            foreach($arr as $v){
                $goods[] = $v;
            }
        }
       // dd($goods);
        return view('home.allcat_list',['goods'=>$goods,'allcat'=>$data]);
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //购物车单个删除
    public function delcart($id)
    {
        //
        DB::table('carts')->where('carts_id',$id)->delete();
        return redirect('home/cart');
    }
    //清空购物车
     public function delallcart()
    {
        //
        $user = Session::get('user')[0];
        DB::table('carts')->where('user_id',$user['id'])->delete();
        return redirect('home/cart');
    }
    //加入购物车
    public function cart($id,$num)
    {
        $user = Session::get('user')[0];
        $count = DB::table('carts')->where('goods_id',$id)->where('user_id',$user['id'])->count();
        if(!$count){
            $data['num']       = $num;
            $data['goods_id']  = $id;
            $data['user_id']   = $user['id'];
            DB::table('carts')->insert($data);
        }else{
            $data = DB::table('carts')->where('goods_id',$id)->where('user_id',$user['id'])->first();
            $num = intval($num);
           /* var_dump($num);
            die;*/
            $n = $num+$data->num;
           //dd($n);
            DB::table('carts')->where('goods_id',$id)->where('user_id',$user['id'])->update(['num'=>$n]);
        }

       
       
        return redirect('home/cart');
    }
    //购物车页面
    public function showcart()
    {
        $id = Session::get('user')[0]['id'];
        $data = DB::table('goods')
            ->join('carts','goods.id','=','carts.goods_id')
            ->select('goods.*','carts.*')
            ->where('carts.user_id',$id)
            ->orderBy('carts.carts_id','desc')
            ->get(); 



        return view('home.cart_list',['goods'=>$data]);
    }
    //创建订单
    public function orders(Request $request, $id)
    {
         //首先插入用户订单表
        //dd($id);
         $user_id         =  Session::get('user')[0]['id'];
         $data['user_id'] =  $user_id;
         $data['status']  =  0;
         $data['time']    =  time();
         $data['send']    =  0;
         //dd($data);
         $orders_id = DB::table('orders')->insertGetId($data);



       // dd($request->items);
      //循环插入订单详情表
        foreach($request->items as $goods_id){
        
         //再插入订单详情表
         $cart = DB::table('carts')->where('carts_id',$goods_id)->first();
         DB::table('carts')->where('carts_id',$goods_id)->delete();
         $data1['orders_id']  =  $orders_id;
         $data1['goods_id']   =  $cart->goods_id;
         $data1['num']  =  $cart->num;
         DB::table('orders_info')->insert($data1);
        }
        //订单成功成功跳转页面
        $orders = DB::table('orders_info')
                ->join('goods','goods.id','=','orders_info.goods_id')
                ->select('goods.*','orders_info.*')
                ->where('orders_info.orders_id',$orders_id)
                ->get();

        
        $user =  Session::get('user')[0];
        return view('home.orders',['orders'=>$orders,'total'=>$id,'orders_id'=>$orders_id,'user'=>$user]);

    }
    //进行结算
    public function pay($id){
        $user =  Session::get('user')[0];
        return view('home.pay',['id'=>$id,'user'=>$user]);
    }
    //结算成功
    public function success(Request $request,$id){
        $user =  Session::get('user')[0];
        $password = md5($request->pwd);
        if($password != $user['password']){
            return redirect("home/pay/$id");
        }
        DB::table('orders')->where('id',$id)->update(['status'=>1]);
        return view('home.success',['user'=>$user]);
    }
}
